﻿using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using System.Collections.Generic;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using System.Windows.Controls;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class TeamMaterialMonthPlanCopy : EFViewCmes
    {
        public TeamMaterialMonthPlanCopy()
        {
            InitializeComponent();
            base.Mode = OperationMode.Insert;
        }

        /// <summary>
        /// 关闭按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Return_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 下一步中确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            List<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            if (_list.Count <= 0)
            {
                this.OpenEFWindowAlert("请选择要复制的月计划(一条或多条)！", true);
                return;
            }
            else
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCM22");
                eiInfo.Set("method", "copyLastMonthPlan");

                ResultBlock(eiInfo);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoWorkCompleted);
            }
        }

        /// <summary>
        /// 下一步中关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                CmesWindows _window = this.Parent as CmesWindows;
                _window.DialogResult = true;
                _window.Close();
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void ResultBlock(EIInfo eiInfo)
        {
            EIBlock resultBlock = new EIBlock("result");
            resultBlock.SetColumns(this.grid.EIBlock.GetColumns());
            resultBlock.SetRows(this.grid.GetCheckedEIRows());
            eiInfo.AddBlock(resultBlock);
        }
    }
}
